﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LinkCard.Model;

namespace LinkCard.Dao
{
    public class PaymentLogDao:ITopLinkDao
    {
        public int Add(object obj)
        {
            var rs = 0;
            try
            {
                var db = new TopLinkEntities();
                var objAdd = (PaymentLog)obj;
                db.AddToPaymentLogs(objAdd);
                rs = db.SaveChanges();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return rs;
        }

        public int Update(object obj)
        {
            var rs = 0;
            try
            {
                var db = new TopLinkEntities();
                var objUpdate = (PaymentLog)obj;
                var id = objUpdate.PaymentLogID;
                var oldObj = db.PaymentLogs.FirstOrDefault(t => t.PaymentLogID == id);
                if (oldObj != null)
                {
                    oldObj.PaymentID = objUpdate.PaymentID;
                    oldObj.SupplierID = objUpdate.SupplierID;
                    oldObj.Value = objUpdate.Value;
                    oldObj.Point = objUpdate.Point;
                    oldObj.Address = objUpdate.Address;
                    oldObj.CreateDate = objUpdate.CreateDate;
                    rs = db.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return rs;
        }

        public int Delete(object obj)
        {
            var rs = 0;
            try
            {
                var db = new TopLinkEntities();
                var objDel = (PaymentLog)obj;
                var id = objDel.PaymentLogID;
                var oldObj = db.PaymentLogs.FirstOrDefault(t => t.PaymentLogID == id);
                if (oldObj != null)
                {
                    db.DeleteObject(oldObj);
                    rs = db.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return rs;
        }

        public PaymentLog GetById(long id)
        {
            var ct = new PaymentLog();
            try
            {
                var db = new TopLinkEntities();
                ct = db.PaymentLogs.FirstOrDefault(t => t.PaymentLogID == id);
            }
            catch (Exception)
            {
                throw;
            }
            return ct;
        }

        public List<PaymentLog> GetAll()
        {
            var lst = new List<PaymentLog>();
            try
            {
                var db = new TopLinkEntities();
                lst = db.PaymentLogs.ToList();
            }
            catch (Exception)
            {
                throw;
            }
            return lst;
        }
    }
}
